import config from './config.js';

var requestheader;
let bs_token;
var request = {}
var mobileInfo;
var appVersion;
// #ifdef APP-PLUS
mobileInfo = plus.os.name
appVersion = plus.runtime.version;
// #endif

function authJum(url,type,alertMsg) {
	if (type=='pdd'){
		// #ifdef APP-PLUS
		if (plus.os.name == 'Android') {
			plus.runtime.openURL(url, res => {

				uni.showModal({
					content: "本机未检测到拼多多客户端，是否打开浏览器访问？",
					success: res => {
						if (res.confirm) {
							plus.runtime.openURL(url)
						}
					}
				})
			}, 'com.xunmeng.pinduoduo');
		} else {
			plus.runtime.launchApplication({
				action: "pinduoduo://com.xunmeng.pinduoduo/" + url.replace(/https:\/\/.*\//, "")
			}, res => {
				uni.showModal({
					content: "本机未检测到拼多多客户端，是否打开浏览器访问？",
					success: res => {
						if (res.confirm) {
							plus.runtime.openURL(url)
						}
					}
				})
			})
		}
		// #endif

		/*#ifdef H5*/
		uni.showModal({
			content: alertMsg,
			success: res => {
				if (res.confirm) {
					uni.navigateTo({
						url: '../webView/webView?url=' + encodeURIComponent(url)
					})
				}
			}
		})
		/*#endif*/
	}else if (type=='tb'){
		// #ifdef APP-PLUS
		if (plus.os.name == 'Android') {
			plus.runtime.openURL(tburl, res => {
				uni.showModal({
					content: "本机未检测到淘宝客户端，是否打开浏览器访问？",
					success: res => {
						if (res.confirm) {
							plus.runtime.openURL(url)
						}
					}
				})
			}, 'com.taobao.taobao');
		} else {
			var http = url
			url = url.split('//')[1]
			plus.runtime.openURL('taobao://' + url, function(res) {
				uni.showModal({
					content: "本机未检测到淘宝客户端，是否打开浏览器访问？",
					success: res => {
						if (res.confirm) {
							uni.navigateTo({
								url: '../webView/webView?url=' + encodeURIComponent(http)
							})
						}
					}
				})
			}, 'taobao://');
		}
		// #endif


		/*#ifdef H5*/
		uni.showModal({
			content: alertMsg,
			success: res => {
				if (res.confirm) {
					uni.navigateTo({
						url: '../webView/webView?url=' + encodeURIComponent(url)
					})
				}
			}
		})
		/*#endif*/
	}
}

request.post = function(url, data, requestheader, showlaoding, complete) {
	uni.getNetworkType({
	    success: res => {
	        //console.log(res.networkType);
			if(res.networkType=='none'){
				uni.navigateTo({
					url:'/pages/index/noNet'
				})
			}
	    }
	})
	requestheader = requestheader || "application/x-www-form-urlencoded";
	//console.log("POST-URL：" + url, data);
	if (showlaoding) {
		uni.showLoading({
			title: ''
		})
	}
	try {
		bs_token = uni.getStorageSync('bs_token') || '';
	} catch (e) {
		bs_token = ''
	}
	var startTime = new Date();
	return new Promise((succ, error) => {
		//console.log(config.httpUrl + url,data)
		uni.request({
			url: config.httpUrl + url,
			data: data,
			method: "POST",
			header: {
				"content-type": requestheader,
				"bstoken": bs_token,
				"mobileInfo":mobileInfo,
				"appVersion":appVersion,
				'bsapikey':config.bsApiKey,
			},
			dataType:'json',
			success: function(result) {
				
				if (typeof succ == "function") {
					// console.log(result.data);
					if (result.data.code == 0 ) {
						succ(result.data.data)
					} else if (result.data.code == 401) {
						uni.navigateTo({
							url:'/pages/login/login'
						})
					} else if (result.data.code == 3002 || result.data.code == 3001) {
						return authJum(result.data.data.url,result.data.code==3001?'tb':'pdd',result.data.msg)
					} else if (result.data.code == -1) {
						uni.showToast({
							title: result.data.msg,
							icon: 'none',
							position: 'bottom',
							duration: 3000
						});
					} else {
						error(result.data.data)
						uni.showToast({
							title: result.data.msg,
							icon: 'none',
							position: 'bottom',
							duration: 3000
						});
					}
				}
			},
			fail: function(e) {
				
				if (typeof error == "function") {
					error(e)
				}
			},
			complete: function(c) {
				uni.hideLoading()
				var endTime = new Date();
				// console.log('request time ' + new Date(endTime - startTime).getTime());
				//console.log('typeof complete',typeof complete)
				if (typeof complete == "function") {
					complete(c.data);
				}
			}
		})
	}).catch((err)=>{
		console.log('post request error',err)
	})
}


request.get = function(url, data, showlaoding) {
	requestheader = requestheader || "application/x-www-form-urlencoded";
	if (showlaoding) {
		uni.showLoading({
			title: ''
		})
	}
	try {
		bs_token = uni.getStorageSync('bs_token') || '';
	} catch (e) {
		bs_token = ''
	}
	return new Promise((succ, error) => {
		uni.request({
			url: config.httpUrl + url,
			data: data,
			method: "GET",
			header: {
				"content-type": requestheader,
				"bstoken": bs_token,
				"mobileInfo":mobileInfo,
				"appVersion":appVersion,
				'bsapikey':config.bsApiKey,
			},
			dataType:'json',
			success: function(result) {
				uni.hideLoading()
				if (result.data.code == 0 ) {
					if (typeof succ == "function") {
						succ(result.data.data)
					}
				} else if (result.data.code == 401) {
					uni.navigateTo({
						url:'/pages/login/login'
					})
				} else if (result.data.code == -1) {
					uni.showToast({
						title: result.data.msg,
						icon: 'none',
						position: 'bottom',
						duration: 3000
					});
				}  else if (result.data.code == 3002 || result.data.code == 3001) {
					return authJum(result.data.data.url,result.data.code==3001?'tb':'pdd',result.data.msg)
				} else {
					error(result.data.data)
					uni.showToast({
						title: result.data.msg,
						icon: 'none',
						position: 'bottom',
						duration: 3000
					});
				}
				
			},
			fail: function(e) {
				uni.hideLoading()
				if (typeof error == "function") {
					error(e)
				}
			},
		})
	}).catch((err)=>{
		console.log('get request error',[config.httpUrl + url,err])
	})
}

export default request
